package com.base.controller;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.po.voip.*;
import com.base.service.RechargeablecardsGiftService;
import com.base.utils.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.comm.CommUrls;
import com.base.po.sqlparam.RechargeablecardsExamplePage;
import com.base.po.sqlparam.UsersExamplePage;
import com.base.po.voip.RechargeablecardsExample.Criteria;
import com.base.service.LogsService;
import com.base.service.RechargeablecardsService;
import com.base.service.UsersService;
import com.base.utils.IP;
import com.base.utils.Page;

@Controller
@RequestMapping("/admin")
public class content_huabo {
	@Autowired
	private RechargeablecardsService rechargeablecardsService;
	@Autowired
	private RechargeablecardsGiftService rechargeablecardsGiftService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private LogsService logsService;// log日志

	// 充值卡管理
	@RequestMapping("/content_huaboOld")
	public ModelAndView index(HttpServletRequest request,
			@RequestParam(value = "choose", required = true) Integer choose) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		RechargeablecardsExamplePage examplePage = new RechargeablecardsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CardNo");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		Users user = usersService.selectByPrimaryKey(adminUserId);
		Criteria criteria = examplePage.createCriteria();
		// UsersExample userExample = new UsersExample();
		switch (adminUsergrade) {
		case 0:
			break;

		case 1:
			// 查询OEM下的商户以及OEM
			List<String> list = usersService.selectByParentId(adminUserId);
			System.out.println("Users的数量：" + list.size());
			for (String oemId : list) {
				// OEM下的商家
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;

		case 2:
			List<String> lists = usersService.selectByShopId(adminUserId);
			System.out.println("Users的数量：" + lists.size());
			for (String shopId : lists) {
				// 商家下的商家
				examplePage.or(examplePage.createCriteria().andParentidEqualTo(shopId));
			}
			examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
			break;
		}

		rechargeablecards = rechargeablecardsService.selectByExamplePage(examplePage);
		for (Rechargeablecards r : rechargeablecards) {
			// 设置OEM以及所属商家的账户名
			Users parent = usersService.selectByPrimaryKey(r.getParentid());
			if (parent != null && parent.getUsername() != null) {
				r.setParentid(parent.getUsername());
			} else {
				r.setParentid("未知");
			}

		}
		/*
		 * 设置分页
		 */
		int totlenum = rechargeablecardsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 传入session中
		request.setAttribute("curPage", 1);// 传入session中
		// request.setAttribute("users", list);// 传入session中
		if (rechargeablecards != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huabo");
			request.setAttribute("list", rechargeablecards);
			request.setAttribute("choose", choose);
			request.setAttribute("oemUsername", user.getUsername());
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "批量修改充值卡信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/content_huabo")
	public ModelAndView indexTest(HttpServletRequest request,
			@RequestParam(value = "choose", required = true) Integer choose) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		// UsersExample userExample = new UsersExample();
		Users user = usersService.selectByPrimaryKey(adminUserId);
		Page page = new Page(); 
		switch (adminUsergrade) {
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(rechargeablecardsService.countByAllNew(null, null, null, null, null, null, null, null, null, null, null, null, null));
			rechargeablecards = rechargeablecardsService.selectByAllNew(page, null, null, null, null, null, null, null, null, null, null, null, null, null);
			break;
		case 1:
			page.setCurrentPageNo(1);
			page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,null, null,null, null, null, null, null, null, null, null, null, null, null));
			rechargeablecards = rechargeablecardsService.selectByParentid(page,adminUserId, null, null, null, null, null, null, null, null, null, null, null, null, null);
			break;
		case 2:
			page.setCurrentPageNo(1);
			page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,null, null,null, null, null, null, null, null, null, null, null, null, null));
			rechargeablecards = rechargeablecardsService.selectByShopidNotAll(page,adminUserId, null, null, null, null, null, null, null, null, null, null, null, null, null);
		}
//		for (Rechargeablecards r : rechargeablecards) {
//			// 设置OEM以及所属商家的账户名
//			Users parent = usersService.selectByPrimaryKey(r.getParentid());
//			if (parent != null && parent.getUsername() != null) {
//				r.setParentid(parent.getUsername());
//			} else {
//				r.setParentid("未知");
//			}
//
//		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (rechargeablecards != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huabo");
			request.setAttribute("list", rechargeablecards);
			request.setAttribute("choose", choose);
			request.setAttribute("oemUsername", user.getUsername());
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "批量修改充值卡信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	// 充值卡管理
	@RequestMapping("/selectChongzhikaHuaboOld")
	public ModelAndView selectChongzhika(HttpServletRequest request, String batch, // 批次
			Integer curPage, // 分页
			String cardnoFrom, String cardnoTo, @RequestParam(value = "choose", required = true) Integer choose)
			throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}

		if (cardnoFrom == null) {
			if (request.getSession().getAttribute("cardnoFrom") != null) {
				cardnoFrom = (String) request.getSession().getAttribute("cardnoFrom");
			}
		}

		if (cardnoTo == null) {
			if (request.getSession().getAttribute("cardnoTo") != null) {
				cardnoTo = (String) request.getSession().getAttribute("cardnoTo");
			}
		}

		if (batch == null) {
			if (request.getSession().getAttribute("batch") != null) {
				batch = (String) request.getSession().getAttribute("batch");
			}
		}

		// 条件查询or查询全部
		Integer findChoose = 1;
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")
				|| batch != null && !batch.equals("")) {
			findChoose = 1;
		} else {
			findChoose = 2;
		}
		//
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		RechargeablecardsExamplePage examplePage = new RechargeablecardsExamplePage();
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CardNo");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		Criteria criteria = examplePage.createCriteria();

		if (adminUsergrade == 0) {
			// 条件筛选
			if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
				request.setAttribute("cardnoFrom", cardnoFrom);
				request.setAttribute("cardnoTo", cardnoTo);
				criteria = criteria.andCardnoBetween(cardnoFrom, cardnoTo);
				examplePage.getOredCriteria().add(criteria);
			}
			if (batch != null && !batch.equals("")) {
				request.setAttribute("batch", batch);
				criteria = criteria.andBatchLike("%" + batch + "%");
				examplePage.getOredCriteria().add(criteria);
			}
		}

		if (adminUsergrade == 1) {
			List<String> list = usersService.selectByParentId(adminUserId);
			// 条件筛选
			if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
				request.setAttribute("cardnoFrom", cardnoFrom);
				request.setAttribute("cardnoTo", cardnoTo);
				// 查询OEM下的商家
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andCardnoBetween(cardnoFrom,
							cardnoTo));
				}
				examplePage.getOredCriteria()
						.add(criteria.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
			}
			if (batch != null && !batch.equals("")) {
				request.setAttribute("batch", batch);
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andBatchEqualTo(batch));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId).andBatchEqualTo(batch));
			}
		}
		if (adminUsergrade == 2) {
			//shopid=adminuserid的所有商家
			List<String> list = usersService.selectByShopId(adminUserId);
			// 条件筛选
			if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
				request.setAttribute("cardnoFrom", cardnoFrom);
				request.setAttribute("cardnoTo", cardnoTo);
				// 查询OEM下的商家
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andCardnoBetween(cardnoFrom,
							cardnoTo));
				}
				examplePage.getOredCriteria()
						.add(criteria.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
			}
			if (batch != null && !batch.equals("")) {
				request.setAttribute("batch", batch);
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId).andBatchEqualTo(batch));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId).andBatchEqualTo(batch));
			}
		}
		if (findChoose == 2) {
			switch (adminUsergrade) {
			case 0:

				break;

			case 1:
				// 查询OEMID下的用户
				// 查询OEM下的商户以及OEM
				List<String> list = usersService.selectByParentId(adminUserId);
				System.out.println("Users的数量：" + list.size());
				for (String oemId : list) {
					// OEM下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(oemId));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
				break;
			case 2:
				// 查询商家下的ID
				List<String> lists = usersService.selectByShopId(adminUserId);
				System.out.println("Users的数量：" + lists.size());
				for (String shopId : lists) {
					// 商家下的商家
					examplePage.or(examplePage.createCriteria().andParentidEqualTo(shopId));
				}
				examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
				break;
			}
		}

		rechargeablecards = rechargeablecardsService.selectByExamplePage(examplePage);
		for (Rechargeablecards r : rechargeablecards) {
			// 设置OEM以及所属商家的账户名
			Users parent = usersService.selectByPrimaryKey(r.getParentid());
			if (parent != null && parent.getUsername() != null) {
				r.setParentid(parent.getUsername());
			} else {
				r.setParentid("未知");
			}

		}
		/*
		 * 设置分页
		 */
		int totlenum = rechargeablecardsService.countByExample(examplePage);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		request.setAttribute("curPage", curPage);// 当前页面传入session中
		if (rechargeablecards != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huabo");
			request.setAttribute("cardnoFrom", cardnoFrom);
			request.setAttribute("cardnoTo", cardnoTo);
			request.setAttribute("batch", batch);
			request.setAttribute("list", rechargeablecards);
			request.setAttribute("choose", choose);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	@RequestMapping("/selectChongzhikaHuabo")
	public ModelAndView selectChongzhikatest(HttpServletRequest request, String batch, // 批次
			Integer curPage, // 分页
			Integer status, // 充值卡状态
			String pId, // 代理商
			String cardnoFrom, String cardnoTo,Integer choose)
			throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}

		if (cardnoFrom == null) {
			if (request.getSession().getAttribute("cardnoFrom") != null) {
				cardnoFrom = (String) request.getSession().getAttribute("cardnoFrom");
			}
		}

		if (cardnoTo == null) {
			if (request.getSession().getAttribute("cardnoTo") != null) {
				cardnoTo = (String) request.getSession().getAttribute("cardnoTo");
			}
		}

		if (batch == null) {
			if (request.getSession().getAttribute("batch") != null) {
				batch = (String) request.getSession().getAttribute("batch");
			}
		}

		// 条件查询or查询全部
		Integer findChoose = 1;
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")
				|| batch != null && !batch.equals("") || status != null 
				|| pId != null && !pId.equals("")) {
			findChoose = 1;
		} else {
			findChoose = 2;
		}
		//
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		
		String sCardno = null;
		Integer sMoney = null ;// 面值
		String parentId = null; // 代理商
		String sMobile = null;// 充值号码
		Integer sStatus = null;// 充值卡状态
		Integer sTermofvalidity = null; // 有效期
		Integer sProbationperiod = null;// 使用有效期
		String sBatch = null;  // 批次
		Boolean sDatastatus = null ;// 冻结结果
		String sCardnoFrom =  null ;
		String sCardnoTo = null;
		Integer sType = null;
		String sFeelroute = null;
		
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			sCardnoFrom = cardnoFrom;
			sCardnoTo = cardnoTo;
		}
		if (batch != null && !batch.equals("")) {
			sBatch = batch;
		}
		//TODO admin 根据是否充值过查询
		if (status != null) {
			// 判断当前可用性
			switch (status) {
			case 0:
				
				break;
			case 1:
				//未充值
				sStatus = 0;
				break;
			case 2:
				//已充值
				sStatus = 1;
				break;
			}
		}
		// 判断当前可用性
		if(pId !=null && !pId.equals("")) {
			Users users = usersService.selectByUsername(pId);
			parentId = users.getId();
		}

		System.out.println(status);
		System.out.println(parentId);
		
		Page page = new Page();
		// admin条件筛选
		page.setCurrentPageNo(curPage);
		if (adminUsergrade == 0) {
			// 条件筛选
			page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			rechargeablecards = rechargeablecardsService.selectByAll(page, sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute);
		}
		if (adminUsergrade == 1) {
			page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			rechargeablecards = rechargeablecardsService.selectByParentid(page,adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute);
		}
		if (adminUsergrade == 2) {
			page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			rechargeablecards = rechargeablecardsService.selectByShopidNotAll(page,adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute);
		}
		if (findChoose == 2) {
			switch (adminUsergrade) {
			case 0:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(rechargeablecardsService.countByAll(null, null, null, null, null, null, null, null, null, null, null, null, null));
				rechargeablecards = rechargeablecardsService.selectByAll(page, null, null, null, null, null, null, null, null, null, null, null, null, null);
				break;
			case 1:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,null, null,null, null, null, null, null, null, null, null, null, null, null));
				rechargeablecards = rechargeablecardsService.selectByParentid(page,adminUserId, null, null, null, null, null, null, null, null, null, null, null, null, null);
				break;
			case 2:
				page.setCurrentPageNo(curPage);
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,null, null,null, null, null, null, null, null, null, null, null, null, null));
				rechargeablecards = rechargeablecardsService.selectByShopidNotAll(page,adminUserId, null, null, null, null, null, null, null, null, null, null, null, null, null);
			}
		}
//		for (Rechargeablecards r : rechargeablecards) {
//			// 设置OEM以及所属商家的账户名
//			Users parent = usersService.selectByPrimaryKey(r.getParentid());
//			if (parent != null && parent.getUsername() != null) {
//				r.setParentid(parent.getUsername());
//			} else {
//				r.setParentid("未知");
//			}
//
//		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		if (rechargeablecards != null) {
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_huabo");
			request.setAttribute("cardnoFrom", cardnoFrom);
			request.setAttribute("cardnoTo", cardnoTo);
			request.setAttribute("batch", batch);
			request.setAttribute("pId", pId);
			request.setAttribute("status", status);
			request.setAttribute("list", rechargeablecards);
			request.setAttribute("choose", choose);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	String SUCCESS = "success";

	// 批量划拨/回收卡
	@RequestMapping(value = "/updateCardsByJDBCOld", method = RequestMethod.POST)
	public void updateCardsByJDBCOld(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String cardno, Integer type, Integer status,
			String feelroute, Integer termofvalidity, Integer probationperiod, Integer money, String parentid,
			String cardnoFrom, String cardnoTo, String batch, Boolean datastatus, Date createtime, Date usetime,
			String mobile, String picpath, String number, String pwd,
			@RequestParam(value = "choose", required = true) Integer choose) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null || parentid == null) {
			out.print("请刷新页面重试！");
			return;
		}
		
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		// 传入的parentid是username
		//划拨 choose == 1
		System.out.println(parentid +"/////test");
		
		if(choose == 1) {
			List<Users> u = usersService.selectByUserName(parentid);
			
			for(Users us : u) {
				System.out.println(us.getUsername());
			}
			if(u.size()>0) {
				if (u.get(0) != null) {
					parentid = u.get(0).getId();
				} else {
					if (choose == 1) {
						out.print("该账户不存在");
						return;
					}
				}
			}else {
				out.print("该账户不存在或者输入的账户不正确");
				return;
			}
		}
		// 回收 choose == 2
		if (choose == 2) {

			if (adminUsergrade != 1) {
				out.print("只有oem能回收");
				return;
			}

			parentid = adminUserId;
		}
		Users aUsers = usersService.selectByPrimaryKey(adminUserId);
		Users pUsers = usersService.selectByPrimaryKey(parentid);

		Connection conn = null;
		PreparedStatement pstm = null;
		
		if(cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			RechargeablecardsExample example = new RechargeablecardsExample();
			Criteria a = example.createCriteria();
			//划拨
			if(adminUsergrade == 0 && choose == 1) {
				a.andCardnoBetween(cardnoFrom, cardnoTo);
			}
			//TODO 
//			if(adminUsergrade == 1 && choose == 1|| adminUsergrade == 2 && choose == 1) {
//				a.andCardnoBetween(cardnoFrom, cardnoTo).andParentidEqualTo(adminUserId);
//			}
			if(adminUsergrade == 1 && choose == 1) {
				List<String> list = usersService.selectByParentId(adminUserId);
				System.out.println("Users的数量：" + list.size());
				for (String oemId : list) {
					// OEM下的商家
					example.or(example.createCriteria().andParentidEqualTo(oemId).andCardnoBetween(cardnoFrom, cardnoTo));
				}
				example.getOredCriteria().add(a.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
			}
			if(adminUsergrade == 2 && choose == 1) {
				a.andCardnoBetween(cardnoFrom, cardnoTo).andParentidEqualTo(adminUserId);
			}
			//回收
		    if(adminUsergrade == 1 && choose == 2) {
		    	List<String> list = usersService.selectByParentId(adminUserId);
				System.out.println("Users的数量：" + list.size());
				for (String oemId : list) {
					// OEM下的商家
					example.or(example.createCriteria().andParentidEqualTo(oemId).andCardnoBetween(cardnoFrom, cardnoTo));
				}
				example.getOredCriteria().add(a.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
		    }
			int count = rechargeablecardsService.countByExample(example);
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					String sql = "update rechargeablecards set parentid = ?  where CardNo BETWEEN  ? AND ? ";
					pstm = conn.prepareStatement(sql);
					pstm.setString(1, pUsers.getId());
					pstm.setString(2,cardnoFrom);
					pstm.setString(3,cardnoTo);
					pstm.executeUpdate();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");
					if (choose == 1) {
						System.out.println("用户：" + aUsers.getUsername() + "划拨卡密,卡号从：" + cardnoFrom + "到" + cardnoTo +  ",数量："
								+ count + ",时间" + nowTime + ",代理商" + pUsers.getUsername());
						logsService.insert(CommContent.TYPE_DELET_HUABO,
								"用户：" + aUsers.getUsername() + "划拨卡密,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量：" + count
										+ ",时间" + nowTime + ",到" + pUsers.getUsername(),
										aUsers.getUsername(), true, IP.getIpAddr(request));
						out.print("共" + count + "张，代理商：" + pUsers.getUsername() + ",划拨成功");
					}
					if (choose == 2) {
						System.out.println("用户：" + aUsers.getUsername() + "回收卡密,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量："
								+ count + ",时间" + nowTime);
						logsService.insert(
								CommContent.TYPE_DELET_HUISHOU, "用户：" + aUsers.getUsername() + "回收卡密,卡号从：" + cardnoFrom + "到" + cardnoTo +  ",数量：" + count + ",时间" + nowTime,
								usernamesys, true, IP.getIpAddr(request));
						out.print("共" + count + "张 ,回收成功");
					}

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确卡号段或属于你的卡号段");
				return;
			}
		}
		
		if(batch != null && !batch.equals("")) {
			RechargeablecardsExample example = new RechargeablecardsExample();
			Criteria a = example.createCriteria();
			//划拨
			if(adminUsergrade == 0) {
				a.andBatchEqualTo(batch);
			}
			if(adminUsergrade == 1 && choose == 1 || adminUsergrade == 2 && choose == 1) {
				a.andBatchEqualTo(batch).andParentidEqualTo(adminUserId);
			}
			//回收
			if(adminUsergrade == 1 && choose == 2) {
		    	List<String> list = usersService.selectByParentId(adminUserId);
				System.out.println("Users的数量：" + list.size());
				for (String oemId : list) {
					// OEM下的商家
					example.or(example.createCriteria().andParentidEqualTo(oemId).andCardnoBetween(cardnoFrom, cardnoTo));
				}
				example.getOredCriteria().add(a.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
		    }
			int count = rechargeablecardsService.countByExample(example);
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					String sql = "update rechargeablecards set parentid = ?  where Batch = ?";
					pstm = conn.prepareStatement(sql);
					pstm.setString(1, pUsers.getId());
					pstm.setString(2, batch);
					pstm.executeUpdate();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");
					if (choose == 1) {
						System.out.println("用户：" + aUsers.getUsername() + "划拨卡密,批次号：" + batch +",数量："
								+ count + ",时间" + nowTime + ",代理商" + pUsers.getUsername());
						logsService.insert(CommContent.TYPE_DELET_HUABO,
								"用户：" + aUsers.getUsername() + "划拨卡密,批次号：" + batch +",数量：" + count
										+ ",时间" + nowTime + ",到" + pUsers.getUsername(),
										aUsers.getUsername(), true, IP.getIpAddr(request));
						out.print("共" + count + "张，代理商：" + pUsers.getUsername() + ",划拨成功");
					}
					if (choose == 2) {
						System.out.println("用户：" + aUsers.getUsername() + "回收卡密,批次号：" + batch +",数量："
								+ count + ",时间" + nowTime);
						logsService.insert(
								CommContent.TYPE_DELET_HUISHOU, "用户：" + aUsers.getUsername() + "回收卡密,批次号：" + batch +",数量：" + count + ",时间" + nowTime,
								usernamesys, true, IP.getIpAddr(request));
						out.print(SUCCESS);
					}

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确批次号或属于你的批次号");
				return;
			}
		}
		

		// } else {
		// out.print("您没有修改卡的权限");
		// return;
		// }
	}
	
	@SuppressWarnings("resource")
	@RequestMapping(value = "/updateCardsByJDBC", method = RequestMethod.POST)
	public void updateCardsByJDBC(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String cardno, Integer type, Integer status,
			String feelroute, Integer termofvalidity, Integer probationperiod, Integer money, String parentid,
			String cardnoFrom, String cardnoTo, String batch, Boolean datastatus, Date createtime, Date usetime,
			String mobile, String picpath, String number, String pwd,String pId,
			@RequestParam(value = "choose", required = true) Integer choose) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null || parentid == null) {
			out.print("请刷新页面重试！");
			return;
		}

		if (cardnoFrom.length() < 9 || cardnoTo.length() < 9){
			out.print("卡号段过短");
			return;
		}

		//划拨 choose == 1
		if(choose == 1) {
			List<Users> u = usersService.selectByUserName(parentid);
			
//			Users us =u
			
//			for(Users us : u) {
//				System.out.println(us.getUsername());
//			}
			if(u.size()>0) {
//				if(u.get(0) !=null) {
//					if(adminUsergrade == 1) {
//						if(!u.get(0).getParentid().equals(adminUserId)) {
//							out.print("该账户不属于当前代理商");
//							return;
//						}
//					}
//					if(adminUsergrade == 2) {
//						if(!u.get(0).getShopid().equals(adminUserId) || !u.get(0).getId().equals(adminUserId)) {
//							out.print("该账户不属于当前商家");
//							return;
//						}
//					}
//				}
				if (u.get(0) != null) {
					parentid = u.get(0).getId();
				} else {
					if (choose == 1) {
						out.print("该账户不存在");
						return;
					}
				}
			}else {
				out.print("该账户不存在或者输入的账户不正确");
				return;
			}
		}
		
			
		// 回收 choose == 2
		if (choose == 2) {

			if (adminUsergrade != 1) {
				out.print("只有oem能回收");
				return;
			}

			parentid = adminUserId;
		}
		Users aUsers = usersService.selectByPrimaryKey(adminUserId);
		Users pUsers = usersService.selectByPrimaryKey(parentid);

		Connection conn = null;
		PreparedStatement pstm = null;
		
		String sCardno = null;
		Integer sMoney = null ;// 面值
		String parentId = null; // 代理商
		String sMobile = null;// 充值号码
		Integer sStatus = null;// 充值卡状态
		Integer sTermofvalidity = null; // 有效期
		Integer sProbationperiod = null;// 使用有效期
		String sBatch = null;  // 批次
		Boolean sDatastatus = null ;// 冻结结果
		String sCardnoFrom =  null ;
		String sCardnoTo = null;
		Integer sType = null;
		String sFeelroute = null;
		
		if (cardno != null && !cardno.equals("")) {
			sCardno = cardno;
		}
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			sCardnoFrom = cardnoFrom;
			sCardnoTo = cardnoTo;
		}
		if (money != null) {
			sMoney = money;
		}
		if (mobile != null && !mobile.equals("")) {
			sMobile = mobile;
		}
		if (termofvalidity != null) {
			sTermofvalidity = termofvalidity;
		}
		if (probationperiod != null) {
		    sProbationperiod = probationperiod;
		}
		if (batch != null && !batch.equals("")) {
			sBatch = batch;
		}
		if (datastatus != null) {
		    sDatastatus = datastatus;
		}
		//TODO admin 根据是否充值过查询
		if (status != null) {
			// 判断当前可用性
			switch (status) {
			case 0:
				
				break;
			case 1:
				//未充值
				sStatus = 0;
				break;
			case 2:
				//已充值
				sStatus = 1;
				break;
			}
		}
		//条件查询的id	
		if(pId !=null && !pId.equals("")) {
			Users users = usersService.selectByUsername(pId);
			parentId = users.getId();
		}
		Page page = new Page();
//		List<Rechargeablecards> rechargeablecards = null;
		if(cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			page.setCurrentPageNo(1);
			//admin划拨 
			if(adminUsergrade == 0 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			// oem划拨
			if(adminUsergrade == 1 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			//商家划拨
			if(adminUsergrade == 2 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			//回收
		    if(adminUsergrade == 1 && choose == 2) {
		    	page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
		    }
			int count = page.getRecordTotal();
			System.out.println(count);
			if (count > 0) {
				if (count > 10000){
					out.print("您输入的回收数量过大，请输入1万或者1万内的数量");
					return;
				}
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					
					if(sStatus == null && parentId == null) {
						String sql = "update rechargeablecards set parentid = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setString(2,cardnoFrom);
						pstm.setString(3,cardnoTo);
						pstm.executeUpdate();
					}
					if(sStatus !=null) {
						String sql = "update rechargeablecards set parentid = ?  where Status = ? AND CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setInt(2, sStatus);
						pstm.setString(3,cardnoFrom);
						pstm.setString(4,cardnoTo);
						pstm.executeUpdate();
					}
					if(parentId !=null) {
						String sql = "update rechargeablecards set parentid = ?  where parentid = ? AND CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setString(2, parentId);
						pstm.setString(3,cardnoFrom);
						pstm.setString(4,cardnoTo);
						pstm.executeUpdate();
					}
					if(sStatus !=null && parentId !=null) {
						String sql = "update rechargeablecards set parentid = ?  where parentid = ? AND Status = ? AND CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setString(2, parentId);
						pstm.setInt(3, sStatus);
						pstm.setString(4,cardnoFrom);
						pstm.setString(5,cardnoTo);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");
					if (choose == 1) {
						System.out.println("用户：" + aUsers.getUsername() + "划拨卡密,卡号从：" + cardnoFrom + "到" + cardnoTo +  ",数量："
								+ count + ",时间" + nowTime + ",代理商" + pUsers.getUsername());
						logsService.insert(CommContent.TYPE_DELET_HUABO,
								"用户：" + aUsers.getUsername() + "划拨卡密,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量：" + count
										+ ",时间" + nowTime + ",到" + pUsers.getUsername(),
								usernamesys, true, IP.getIpAddr(request));
						out.print("共" + count + "张，代理商：" + pUsers.getUsername() + ",划拨成功");
					}
					if (choose == 2) {
						System.out.println("用户：" + aUsers.getUsername() + "回收卡密,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量："
								+ count + ",时间" + nowTime);
						logsService.insert(
								CommContent.TYPE_DELET_HUISHOU, "用户：" + aUsers.getUsername() + "回收卡密,卡号从：" + cardnoFrom + "到" + cardnoTo +  ",数量：" + count + ",时间" + nowTime,
								usernamesys, true, IP.getIpAddr(request));
						out.print("共" + count + "张 ,回收成功");
					}

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确卡号段或属于你的卡号段");
				return;
			}
		}else {
			page.setCurrentPageNo(1);
			//admin划拨 
			if(adminUsergrade == 0 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			// oem划拨
			if(adminUsergrade == 1 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			    System.out.println("3.14");
			}
			//商家划拨
			if(adminUsergrade == 2 && choose == 1) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			//回收
		    if(adminUsergrade == 1 && choose == 2) {
		    	page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
		    }
			int count = page.getRecordTotal();
			if (count > 0) {
				if (count > 10000){
					out.print("您输入的划拨数量过大，请输入1万或者1万内的数量");
					return;
				}
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
			
					if(sStatus !=null) {
						String sql = "update rechargeablecards set parentid = ?  where Status = ?";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setInt(2, sStatus);
						pstm.executeUpdate();
					}
					if(parentId !=null) {
						String sql = "update rechargeablecards set parentid = ?  where parentid = ?";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						System.out.println(pUsers.getId() +"lll");
						pstm.setString(2, parentId);
						System.out.println(parentId +"lll");
						pstm.executeUpdate();
					}
					if(sStatus !=null && parentId !=null) {
						String sql = "update rechargeablecards set parentid = ?  where parentid = ? AND Status = ?";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, pUsers.getId());
						pstm.setString(2, parentId);
						pstm.setInt(3, sStatus);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");
					if (choose == 1) {
						System.out.println("用户：" + aUsers.getUsername() + "划拨卡密,卡号所有,数量："
								+ count + ",时间" + nowTime + ",代理商" + pUsers.getUsername());
						logsService.insert(CommContent.TYPE_DELET_HUABO,
								"用户：" + aUsers.getUsername() + "划拨卡密,卡号所有,数量：" + count
										+ ",时间" + nowTime + ",到" + pUsers.getUsername(),
								usernamesys, true, IP.getIpAddr(request));
						out.print("共" + count + "张，代理商：" + pUsers.getUsername() + ",划拨成功");
					}
					if (choose == 2) {
						System.out.println("用户：" + aUsers.getUsername() + "回收卡密,卡号所有,数量："
								+ count + ",时间" + nowTime);
						logsService.insert(
								CommContent.TYPE_DELET_HUISHOU, "用户：" + aUsers.getUsername() + "回收卡密,卡号所有,数量：" + count + ",时间" + nowTime,
								usernamesys, true, IP.getIpAddr(request));
						out.print("共" + count + "张 ,回收成功");
					}

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确卡号段或属于你的卡号段");
				return;
			}
		}
		
//		if(batch != null && !batch.equals("")) {
//			page.setCurrentPageNo(1);
//			//admin划拨 
//			if(adminUsergrade == 0 && choose == 1) {
//				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
//			}
//			// oem划拨
//			if(adminUsergrade == 1 && choose == 1) {
//				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
//			}
//			//商家划拨
//			if(adminUsergrade == 2 && choose == 1) {
//				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
//			}
//			//回收
//		    if(adminUsergrade == 1 && choose == 2) {
//		    	page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
//		    }
//			int count = page.getRecordTotal();
//			if (count > 0) {
//				try {
//					Class.forName("com.mysql.jdbc.Driver");
//					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
//					
//					if(sStatus == null && parentId == null) {
//						String sql = "update rechargeablecards set parentid = ?  where Batch = ?";
//						pstm = conn.prepareStatement(sql);
//						pstm.setString(1, pUsers.getId());
//						pstm.setString(2, batch);
//						pstm.executeUpdate();
//					}
//					if(sStatus !=null) {
//						String sql = "update rechargeablecards set parentid = ?  where Status = ? AND Batch = ? ";
//						pstm = conn.prepareStatement(sql);
//						pstm.setString(1, pUsers.getId());
//						pstm.setInt(2, sStatus);
//						pstm.setString(3, batch);
//						pstm.executeUpdate();
//					}
//					if(parentId !=null) {
//						String sql = "update rechargeablecards set parentid = ?  where parentid = ? AND Batch = ? ";
//						pstm = conn.prepareStatement(sql);
//						pstm.setString(1, pUsers.getId());
//						pstm.setString(2, parentId);
//						pstm.setString(3, batch);
//						pstm.executeUpdate();
//					}
//					if(sStatus !=null && parentId !=null) {
//						String sql = "update rechargeablecards set parentid = ?  where parentid = ? AND Status = ? AND Batch = ? ";
//						pstm = conn.prepareStatement(sql);
//						pstm.setString(1, pUsers.getId());
//						pstm.setString(2, parentId);
//						pstm.setInt(3, sStatus);
//						pstm.setString(4, batch);
//						pstm.executeUpdate();
//					}
//					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//					String nowTime = sdf.format(new Date());
//					String usernamesys = (String) request.getSession().getAttribute("useracc");
//					if (choose == 1) {
//						System.out.println("用户：" + aUsers.getUsername() + "划拨卡密,批次号：" + batch +",数量："
//								+ count + ",时间" + nowTime + ",代理商" + pUsers.getUsername());
//						logsService.insert(CommContent.TYPE_DELET_HUABO,
//								"用户：" + aUsers.getUsername() + "划拨卡密,批次号：" + batch +",数量：" + count
//										+ ",时间" + nowTime + ",到" + pUsers.getUsername(),
//								usernamesys, true, IP.getIpAddr(request));
//						out.print("共" + count + "张，代理商：" + pUsers.getUsername() + ",划拨成功");
//					}
//					if (choose == 2) {
//						System.out.println("用户：" + aUsers.getUsername() + "回收卡密,批次号：" + batch +",数量："
//								+ count + ",时间" + nowTime);
//						logsService.insert(
//								CommContent.TYPE_DELET_HUISHOU, "用户：" + aUsers.getUsername() + "回收卡密,批次号：" + batch +",数量：" + count + ",时间" + nowTime,
//								usernamesys, true, IP.getIpAddr(request));
//						out.print("共" + count + "张 ,回收成功");
//					}
//
//				} catch (Exception e) {
//					out.print("修改失败，请您重试");
//					e.printStackTrace();
//					throw new RuntimeException(e);
//				} finally {
//					if (pstm != null) {
//						try {
//							pstm.close();
//						} catch (SQLException e) {
//							e.printStackTrace();
//							throw new RuntimeException(e);
//						}
//					}
//					if (conn != null) {
//						try {
//							conn.close();
//						} catch (SQLException e) {
//							out.print("修改失败，请您重试");
//							e.printStackTrace();
//							throw new RuntimeException(e);
//						}
//					}
//				}
//			} else {
//				out.print("请输入正确批次号或属于你的批次号");
//				return;
//			}
//		}
		

		// } else {
		// out.print("您没有修改卡的权限");
		// return;
		// }
	}

	// 批量修改
	@SuppressWarnings("resource")
	@RequestMapping(value = "/xiugaiCardsByJDBC", method = RequestMethod.POST)
	public void xiugaiCardsByJDBC(HttpServletRequest request, HttpServletResponse response, String cardno, Integer type,
			Integer status, String feelroute, Integer termofvalidity, Integer probationperiod, Integer money, Integer shopmoney,
			String parentid, String cardnoFrom, String cardnoTo, String batch, Boolean datastatus, Date createtime,
			Date usetime, String mobile, String picpath, String number, String pwd, Integer addtermofvalidity,
			Byte isupdateshop) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		// 传入的parentid是username
		Users aUsers = usersService.selectByPrimaryKey(adminUserId);

        if(feelroute == null || "".equals(feelroute)) {
        	feelroute = null;
        }
        
		Connection conn = null;
		PreparedStatement pstm = null;
		
		String sCardno = null;
		Integer sMoney = null ;// 面值
		String parentId = null; // 代理商
		String sMobile = null;// 充值号码
		Integer sStatus = null;// 充值卡状态
		Integer sTermofvalidity = null; // 有效期
		Integer sProbationperiod = null;// 使用有效期
		String sBatch = null;  // 批次
		Boolean sDatastatus = null ;// 冻结结果
		String sCardnoFrom =  null ;
		String sCardnoTo = null;
		Integer sType = null;
		String sFeelroute = null;
		
		Page page = new Page();
		//根据卡号段修改
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			sCardnoFrom = cardnoFrom;
			sCardnoTo = cardnoTo;
			
			if (adminUsergrade == 0) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 2) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			int count = page.getRecordTotal();
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					if (probationperiod != null) {
						String sql = "update rechargeablecards set probationperiod = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, probationperiod);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (termofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, termofvalidity);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (addtermofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = termofvalidity + ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, addtermofvalidity);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (money != null) {
						String sql = "update rechargeablecards set money = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, money);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (isupdateshop != null) {
						String sql = "update rechargeablecards set isupdateshop = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setByte(1, isupdateshop);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (feelroute != null) {
						String sql = "update rechargeablecards set feelroute = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, feelroute);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					if (datastatus != null) {
						String sql = "update rechargeablecards set datastatus = ?  where CardNo BETWEEN  ? AND ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setBoolean(1, datastatus);
						pstm.setString(2, cardnoFrom);
						pstm.setString(3, cardnoTo);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");

					System.out.println("用户" + aUsers.getUsername() + "修改卡信息,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量："
							+ count + ",时间" + nowTime);
					logsService.insert(CommContent.TYPE_DELET_XIUGAIKAMI,
							"用户：" + aUsers.getUsername() + "修改卡信息,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量：" + count
									+ ",时间" + nowTime ,usernamesys, true, IP.getIpAddr(request));
					out.print("用户" + aUsers.getUsername() + "修改卡信息,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量："
							+ count +",修改成功");

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确卡号段或属于你的卡号段");
				return;
			}
		}
		//根据批次号修改
		if (batch != null && !batch.equals("")) {
			sBatch = batch;
			
			if (adminUsergrade == 0) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 2) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			int count = page.getRecordTotal();
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					if (probationperiod != null) {
						String sql = "update rechargeablecards set probationperiod = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, probationperiod);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (termofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, termofvalidity);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (addtermofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = termofvalidity + ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, addtermofvalidity);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (money != null) {	
						String sql = "update rechargeablecards set money = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, money);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (shopmoney != null) {
						String sql = "update rechargeablecards set Type = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, 7);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (isupdateshop != null) {
						String sql = "update rechargeablecards set isupdateshop = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setByte(1, isupdateshop);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (feelroute != null) {
						String sql = "update rechargeablecards set feelroute = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, feelroute);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					if (datastatus != null) {
						String sql = "update rechargeablecards set datastatus = ?  where Batch = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setBoolean(1, datastatus);
						pstm.setString(2, batch);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");

					System.out.println("用户" + aUsers.getUsername() + "修改卡信息,批次号：" + batch + ",数量："
							+ count + ",时间" + nowTime);
					logsService.insert(
							CommContent.TYPE_DELET_XIUGAIKAMI, "用户：" + aUsers.getUsername() + "修改卡信息,批次号：" + batch + ",数量："
									+ count + ",时间" + nowTime ,usernamesys, true, IP.getIpAddr(request));
					out.print("用户" + aUsers.getUsername() + "修改卡信息,批次号：" + batch + ",数量："
							+ count + ",修改成功");
					if (batch != null && shopmoney != null){
						RechargeablecardsGift gift = rechargeablecardsGiftService.selectByBatch(batch);
						if (gift != null){
							gift.setShopmoney(shopmoney);
                            rechargeablecardsGiftService.updateByPrimaryKeySelective(gift);
						}else{
							rechargeablecardsGiftService.insertSelective(new RechargeablecardsGift(Produce.getUUID(), batch, shopmoney));
						}
					}
				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确批次号或属于你的批次号");
				return;
			}
		}
		
		if (parentid != null && !parentid.equals("")) {
			List<Users> u = usersService.selectByUserName(parentid);
			if(u != null && u.size()>0) {
				parentId = u.get(0).getId();
			}else {
				out.print("该商户不存在");
				return;
			}
			
			if (adminUsergrade == 0) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 2) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			int count = page.getRecordTotal();
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					if (probationperiod != null) {
						String sql = "update rechargeablecards set probationperiod = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, probationperiod);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (termofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, termofvalidity);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (addtermofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = termofvalidity + ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, addtermofvalidity);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (money != null) {	
						String sql = "update rechargeablecards set money = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, money);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (isupdateshop != null) {
						String sql = "update rechargeablecards set isupdateshop = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setByte(1, isupdateshop);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (feelroute != null) {
						String sql = "update rechargeablecards set feelroute = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, feelroute);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					if (datastatus != null) {
						String sql = "update rechargeablecards set datastatus = ?  where ParentID = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setBoolean(1, datastatus);
						pstm.setString(2, parentId);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");

					System.out.println("用户" + aUsers.getUsername() + "修改卡信息,所属商：" + parentid + ",数量："
							+ count + ",时间" + nowTime);
					logsService.insert(
							CommContent.TYPE_DELET_XIUGAIKAMI, "用户：" + aUsers.getUsername() + "修改卡信息,所属商：" + parentid + ",数量："
									+ count + ",时间" + nowTime ,usernamesys, true, IP.getIpAddr(request));
					out.print("用户" + aUsers.getUsername() + "修改卡信息,所属商：" + parentid + ",数量："
							+ count + ",修改成功");

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确所属商或属于你的所属商");
				return;
			}
		}
		
		if (cardno != null && !cardno.equals("")) {
			sCardno = cardno;
			
			if (adminUsergrade == 0) {
				page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 1) {
				page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			if (adminUsergrade == 2) {
				page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
			}
			int count = page.getRecordTotal();
			if (count > 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
					if (probationperiod != null) {
						String sql = "update rechargeablecards set probationperiod = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, probationperiod);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (termofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, termofvalidity);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (addtermofvalidity != null) {
						String sql = "update rechargeablecards set termofvalidity = termofvalidity + ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, addtermofvalidity);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (money != null) {	
						String sql = "update rechargeablecards set money = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setInt(1, money);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (isupdateshop != null) {
						String sql = "update rechargeablecards set isupdateshop = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setByte(1, isupdateshop);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (feelroute != null) {
						String sql = "update rechargeablecards set feelroute = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setString(1, feelroute);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					if (datastatus != null) {
						String sql = "update rechargeablecards set datastatus = ?  where CardNo = ? ";
						pstm = conn.prepareStatement(sql);
						pstm.setBoolean(1, datastatus);
						pstm.setString(2, cardno);
						pstm.executeUpdate();
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
					String nowTime = sdf.format(new Date());
					String usernamesys = (String) request.getSession().getAttribute("useracc");

					System.out.println("用户" + aUsers.getUsername() + "修改卡信息,卡号：" + cardno + ",数量："
							+ count + ",时间" + nowTime);
					logsService.insert(
							CommContent.TYPE_DELET_XIUGAIKAMI, "用户：" + aUsers.getUsername() + "修改卡信息,卡号：" + cardno + ",数量："
									+ count + ",时间" + nowTime ,usernamesys, true, IP.getIpAddr(request));
					out.print("用户" + aUsers.getUsername() + "修改卡信息,卡号：" + cardno + ",数量："
							+ count + ",修改成功");

				} catch (Exception e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				} finally {
					if (pstm != null) {
						try {
							pstm.close();
						} catch (SQLException e) {
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
					if (conn != null) {
						try {
							conn.close();
						} catch (SQLException e) {
							out.print("修改失败，请您重试");
							e.printStackTrace();
							throw new RuntimeException(e);
						}
					}
				}
			} else {
				out.print("请输入正确面号或属于你的卡号");
				return;
			}
		}
	}

	// 批量更新卡
	@RequestMapping(value = "/piUpdateCards", method = RequestMethod.POST)
	public void piUpdateCards(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String cardno, Integer type, Integer status,
			String feelroute, Integer termofvalidity, Integer probationperiod, Integer money, String parentid,
			String cardnoFrom, String cardnoTo, String batch, Boolean datastatus, Date createtime, Date usetime,
			String mobile, String picpath, String number, String pwd,
			@RequestParam(value = "choose", required = true) Integer choose) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null || parentid == null) {
			out.print("请刷新页面重试！");
			return;
		}
		if (cardnoFrom == null || cardnoTo == null || cardnoFrom.equals("") || cardnoTo.equals("")) {
			out.print("请填写卡号段");
			return;
		}
		/**
		 * 查询所有符合条件的卡
		 */
		RechargeablecardsExample examplePage = new RechargeablecardsExample();
		examplePage.setOrderByClause("CardNo");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		Criteria criteria = examplePage.createCriteria();

		UsersExample usersExample = new UsersExample();
		List<Users> usersList = null;
		UsersExample.Criteria b = usersExample.createCriteria();
		b.andParentidEqualTo(adminUserId).andDatastatusEqualTo(true);
		usersList = usersService.selectByExample(usersExample);
		// 传入的parentid是username
		Users u = usersService.selectByUsername(parentid);
		if (u != null) {
			parentid = u.getId();
		} else {
			if (choose == 1) {
				out.print("该账户不存在");
				return;
			}
		}

		// 回收 choose == 2
		if (choose == 2) {

			if (adminUsergrade != 2) {
				out.print("只有oem能回收");
				return;
			}

			parentid = adminUserId;
		}

		Users aUsers = usersService.selectByPrimaryKey(adminUserId);
		Users pUsers = usersService.selectByPrimaryKey(parentid);

		System.out.println(choose);
		System.out.println(parentid);

		// // 条件筛选
		// if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null &&
		// !cardnoTo.equals("")) {
		// request.setAttribute("cardnoFrom", cardnoFrom);
		// request.setAttribute("cardnoTo", cardnoTo);
		// criteria = criteria.andCardnoBetween(cardnoFrom, cardnoTo);
		// examplePage.getOredCriteria().add(criteria);
		// }
		// if (batch != null && !batch.equals("")) {
		// request.setAttribute("batch", batch);
		// criteria = criteria.andBatchLike("%" + batch + "%");
		// examplePage.getOredCriteria().add(criteria);
		// }
		// switch (adminUsergrade) {
		// case 0:
		//
		// break;
		//
		// case 1:
		// // 查询OEMID下的用户
		// // 查询OEM下的商户以及OEM
		// UsersExample userExample = new UsersExamplePage();
		// userExample.getOredCriteria().add(userExample.createCriteria().andParentidEqualTo(adminUserId));
		// List<Users> list = usersService.selectByExample(userExample);
		// for (Users users : list) {
		// examplePage.or(examplePage.createCriteria().andParentidEqualTo(users.getId()));
		// }
		// examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
		// break;
		//
		// case 2:
		// // 查询商家下的ID
		// examplePage.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId));
		// break;
		// }
		// rechargeablecards = rechargeablecardsService.selectByExample(examplePage);
		// 获取最新的卡

		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(CommUrls.JDBC, CommUrls.JDBC_USER, CommUrls.JDBC_PASSWORD);
			String sql = "update rechargeablecards set parentid = ? where id = ?";
			pstm = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			conn.setAutoCommit(false);
			System.out.println(rechargeablecards.size() + "张卡");
			for (Rechargeablecards r : rechargeablecards) {
				pstm.setString(1, parentid);
				pstm.setString(2, r.getId());
				System.out.println(r.getId());
				pstm.addBatch();
				if ((rechargeablecards.size() + 1) % 1000 == 0) {
					pstm.executeBatch();// 每执行一次就清一次sql缓存。
					pstm.clearBatch(); // 否则每一次是执行1000，2000，3000...条sql语句而不是每一次执行1000条
				}
			}
			pstm.executeBatch();
			conn.commit();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String nowTime = sdf.format(new Date());
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			if (choose == 1) {
				logsService.insert(CommContent.TYPE_DELET_HUABO,
						"用户" + aUsers.getUsername() + "划拨卡密,卡号从：" + cardnoFrom + "到" + cardnoTo + ",数量："
								+ rechargeablecards.size() + ",时间" + nowTime + ",代理商" + pUsers.getUsername(),
						usernamesys, true, IP.getIpAddr(request));
				out.print(SUCCESS);
			}
			if (choose == 2) {
				logsService.insert(
						CommContent.TYPE_DELET_HUISHOU, "用户" + aUsers.getUsername() + "回收卡密,卡号从：" + cardnoFrom + "到"
								+ cardnoTo + ",数量：" + rechargeablecards.size() + ",时间" + nowTime,
						usernamesys, true, IP.getIpAddr(request));
				out.print(SUCCESS);
			}

		} catch (Exception e) {
			out.print("修改失败，请您重试");
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					out.print("修改失败，请您重试");
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}

	}

	// 批量更新卡
	@RequestMapping(value = "/updateCards", method = RequestMethod.POST)
	public void updateCards(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String cardno, Integer type, Integer status,
			String feelroute, Integer termofvalidity, Integer probationperiod, Integer money, String parentid,
			String cardnoFrom, String cardnoTo, String batch, Boolean datastatus, Date createtime, Date usetime,
			String mobile, String picpath, String number, String pwd) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		parentid = adminUserId;
		System.out.println(feelroute);
		// Users pUsers = usersService.selectByPrimaryKey(adminUserId);
		// if(pUsers.getUsername() == "admin") {
		// Users aUsers = usersService.selectByUsername("随便打");
		// parentid = aUsers.getId();
		// System.out.println(parentid);
		// }
//		if(feelroute == null || feelroute.equals("")) {
//			feelroute = null;
//		}
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			Rechargeablecards record = new Rechargeablecards(id.trim(), cardno, pwd, money, parentid, status,
					termofvalidity, probationperiod, feelroute, datastatus, batch, createtime, usetime, mobile, picpath,
					type, number);
			int code = rechargeablecardsService.updateByPrimaryKeySelective(record);
			if (code == 1) {
				out.print(SUCCESS);
			} else {
				out.print("更改失败，请您重试");
			}

		}

	}

	// 充值卡数量
	@RequestMapping("/selectTotlenumOld")
	public void selectTotlenumOld(HttpServletRequest request, HttpServletResponse response, String batch, // 批次
			String cardnoFrom, String cardnoTo) throws Exception {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 设置session缓存
		if (cardnoFrom == null) {
			if (request.getSession().getAttribute("cardnoFrom") != null) {
				cardnoFrom = (String) request.getSession().getAttribute("cardnoFrom");
			}
		}

		if (cardnoTo == null) {
			if (request.getSession().getAttribute("cardnoTo") != null) {
				cardnoTo = (String) request.getSession().getAttribute("cardnoTo");
			}
		}

		if (batch == null) {
			if (request.getSession().getAttribute("batch") != null) {
				batch = (String) request.getSession().getAttribute("batch");
			}
		}
		RechargeablecardsExample example = new RechargeablecardsExample();
		example.setOrderByClause("CardNo");
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Rechargeablecards> rechargeablecards = null;
		Criteria criteria = example.createCriteria();

		if (adminUsergrade == 0) {
			// 条件筛选
			if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
				request.setAttribute("cardnoFrom", cardnoFrom);
				request.setAttribute("cardnoTo", cardnoTo);
				criteria = criteria.andCardnoBetween(cardnoFrom, cardnoTo);
				example.getOredCriteria().add(criteria);
			}
			if (batch != null && !batch.equals("")) {
				request.setAttribute("batch", batch);
				criteria = criteria.andBatchLike("%" + batch + "%");
				example.getOredCriteria().add(criteria);
			}
		}
		if (adminUsergrade == 1) {
			UsersExample userExample = new UsersExamplePage();
			userExample.getOredCriteria().add(userExample.createCriteria().andParentidEqualTo(adminUserId));
			List<Users> list = usersService.selectByExample(userExample);
			// 条件筛选
			if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
				request.setAttribute("cardnoFrom", cardnoFrom);
				request.setAttribute("cardnoTo", cardnoTo);
				for (Users users : list) {
					example.or(example.createCriteria().andParentidEqualTo(users.getId()).andCardnoBetween(cardnoFrom,
							cardnoTo));
				}
				example.getOredCriteria()
						.add(criteria.andParentidEqualTo(adminUserId).andCardnoBetween(cardnoFrom, cardnoTo));
			}
			if (batch != null && !batch.equals("")) {
				request.setAttribute("batch", batch);
				for (Users users : list) {
					example.or(
							example.createCriteria().andParentidEqualTo(users.getId()).andBatchLike("%" + batch + "%"));
				}
				example.getOredCriteria().add(criteria.andParentidEqualTo(adminUserId).andBatchLike("%" + batch + "%"));
			}
		}
		rechargeablecards = rechargeablecardsService.selectByExample(example);

		/*
		 * 设置分页
		 */
		int totlenum = rechargeablecardsService.countByExample(example);// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		out.print(totlenum);
	}
	
	@RequestMapping("/selectTotlenum")
	public void selectTotlenum(HttpServletRequest request, HttpServletResponse response, String batch, // 批次
			String cardnoFrom, String cardnoTo) throws Exception {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 设置session缓存
		if (cardnoFrom == null) {
			if (request.getSession().getAttribute("cardnoFrom") != null) {
				cardnoFrom = (String) request.getSession().getAttribute("cardnoFrom");
			}
		}

		if (cardnoTo == null) {
			if (request.getSession().getAttribute("cardnoTo") != null) {
				cardnoTo = (String) request.getSession().getAttribute("cardnoTo");
			}
		}

		if (batch == null) {
			if (request.getSession().getAttribute("batch") != null) {
				batch = (String) request.getSession().getAttribute("batch");
			}
		}
		

		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		String sCardno = null;
		Integer sMoney = null ;// 面值
		String parentId = null; // 代理商
		String sMobile = null;// 充值号码
		Integer sStatus = null;// 充值卡状态
		Integer sTermofvalidity = null; // 有效期
		Integer sProbationperiod = null;// 使用有效期
		String sBatch = null;  // 批次
		Boolean sDatastatus = null ;// 冻结结果
		String sCardnoFrom =  null ;
		String sCardnoTo = null;
		Integer sType = null;
		String sFeelroute = null;
		
		if (cardnoFrom != null && !cardnoFrom.equals("") && cardnoTo != null && !cardnoTo.equals("")) {
			sCardnoFrom = cardnoFrom;
			sCardnoTo = cardnoTo;
		}
		if (batch != null && !batch.equals("")) {
			sBatch = batch;
		}
		
		Page page = new Page();
		page.setCurrentPageNo(1);
		if(adminUsergrade == 0) {
			page.setRecordTotal(rechargeablecardsService.countByAll(sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
		}
		// oem划拨
		if(adminUsergrade == 1) {
			page.setRecordTotal(rechargeablecardsService.countByParentid(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
		}
		//商家划拨
		if(adminUsergrade == 2) {
			page.setRecordTotal(rechargeablecardsService.countByShopidNotAll(adminUserId,sStatus, sBatch, sMoney, sMobile, parentId, sCardno, sTermofvalidity, sProbationperiod, sCardnoFrom, sCardnoTo, sDatastatus, sType, sFeelroute));
		}
		
		int totlenum = page.getRecordTotal();// 页面总数
		request.setAttribute("totlenum", totlenum);// 总数据传入session中
		out.print(totlenum);
	}
}